package com.tang.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.common.entity.community.News;
import com.tang.community.mapper.NewsMapper;
import com.tang.community.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-20
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    private  NewsMapper newsMapper;
    @Override
    public PageInfo<News> listNews(String keywords, Integer currentPage, Integer pageSize, String adminName) {
        if (adminName==null) {
            PageHelper.startPage(currentPage, pageSize);
            if (keywords == null || "".equals(keywords)) {
                QueryWrapper<News> qw = new QueryWrapper<>();
                qw.eq("delete_flag", 1);
                qw.orderByDesc("create_time");
                List<News> newses = newsMapper.selectList(qw);
                return PageInfo.of(newses);
            }
            QueryWrapper<News> queryWrapper = new QueryWrapper<News>();
            queryWrapper.eq("delete_flag", 1).orderByDesc("create_time").
                    like("news_title", keywords).or().like("admin_name",keywords);
            List<News> newses = newsMapper.selectList(queryWrapper);
            return PageInfo.of(newses);
        } else {
            PageHelper.startPage(currentPage, pageSize);
            if (keywords == null || "".equals(keywords)) {
                QueryWrapper<News> qw = new QueryWrapper<>();
                qw.eq("delete_flag", 1);
                qw.eq("admin_name", adminName);
                qw.orderByDesc("create_time");
                List<News> newses = newsMapper.selectList(qw);
                return PageInfo.of(newses);
            }
            QueryWrapper<News> queryWrapper = new QueryWrapper<News>();
            queryWrapper.eq("delete_flag", 1).orderByDesc("create_time").eq("admin_name", adminName).like("news_title", keywords);
            List<News> lives = newsMapper.selectList(queryWrapper);
            return PageInfo.of(lives);
        }
    }
}
